package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.ui.activity.NearBySearchActivity;
import com.clzj.makepassenger.bean.NearbySearchHistory;
import com.clzj.makepassenger.dao.NearbySearchHistoryDao;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.dao.query.Query;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by apple on 2017/4/28.
 */

public class NearBySearchP extends BaseP<NearBySearchActivity> {
//获取搜索历史
    public void getSearchHistory() {
        Observable.just(daoSession.getNearbySearchHistoryDao().queryBuilder().where(NearbySearchHistoryDao.Properties.Token.eq(getToken())).build())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(getV().<Query<NearbySearchHistory>>bindToLifecycle())
                .subscribe(new Action1<Query<NearbySearchHistory>>() {
                    @Override
                    public void call(Query<NearbySearchHistory> nearbySearchHistoryQuery) {
                        List<String> history = new ArrayList<>();
                        List<NearbySearchHistory> historyList = nearbySearchHistoryQuery.list();
                        if (historyList.size() > 0) {
                            for (int i = 0; i < historyList.size(); i++) {
                                history.add(historyList.get(i).getHistory());
                            }
                            getV().showHistory(history);
                        } else {
                            getV().showHistory(null);
                        }
                    }
                });
    }
//设置搜索历史
    public void setSearchHistory(final String key) {

        Observable.just(daoSession.getNearbySearchHistoryDao().queryBuilder().where(NearbySearchHistoryDao.Properties.Token.eq(getToken())).build())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(getV().<Query<NearbySearchHistory>>bindToLifecycle())
                .subscribe(new Action1<Query<NearbySearchHistory>>() {
                    @Override
                    public void call(Query<NearbySearchHistory> nearbySearchHistoryQuery) {
                        List<String> history = new ArrayList<>();
                        List<NearbySearchHistory> historyList = nearbySearchHistoryQuery.list();
                        NearbySearchHistory nearbySearchHistory = new NearbySearchHistory();
                        nearbySearchHistory.setAdd_time(System.currentTimeMillis());
                        nearbySearchHistory.setToken(getToken());
                        nearbySearchHistory.setHistory(key);
                        if (historyList.size() > 0) {
                            for (int i = 0; i < historyList.size(); i++) {
                                history.add(historyList.get(i).getHistory());
                            }

                            if (history.contains(key)) {
                                daoSession.getNearbySearchHistoryDao().update(nearbySearchHistory);
                            } else {
                                daoSession.getNearbySearchHistoryDao().insert(nearbySearchHistory);
                            }
                        } else {
                            daoSession.getNearbySearchHistoryDao().insert(nearbySearchHistory);
                        }
                    }
                });

    }
}
